﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using stdole;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 要素操作类
    /// </summary>
    public class ElementHelper
    {
        #region 颜色
        /// <summary>
        /// 创建IRgbColor对象
        /// </summary>
        /// <param name="r">红0-255</param>
        /// <param name="g">绿0-255</param>
        /// <param name="b">蓝0-255</param>
        /// <param name="transparency">透明0-255</param>
        /// <returns></returns>
        public static IRgbColor CreateRgbColor(int r = 255, int g = 255, int b = 0, byte transparency = 10)
        {
            return new RgbColor()
            {
                Red = 0,
                Blue = 0,
                Green = 0,
                Transparency = transparency
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static ISimpleMarkerSymbol CreateSimpleMarkerSymbol(IRgbColor color = null, double size = 5)
        {
            if (color == null) color = CreateRgbColor();
            return new SimpleMarkerSymbol()
            {
                Size = size,
                Color = color
            };
        }
        #endregion

        #region Geometry
        /// <summary>
        /// 添加空间对象到地图中
        /// </summary>
        /// <param name="geometry">空间对象</param>
        /// <param name="activeView">地图活动视图</param>
        /// <param name="color">颜色</param>
        /// <param name="width">宽度</param>
        public static void AddGeometry(IGeometry geometry, IActiveView activeView, IRgbColor color = null, double width = 1)
        {
            if (color == null)
            {
                color = new RgbColor();
                color.Red = 255;
                color.Green = 255;
                color.Blue = 0;
                color.Transparency = 10;
            }
            ISimpleLineSymbol lineSymbol = new SimpleLineSymbol();
            lineSymbol.Color = color;
            lineSymbol.Width = width;

            IDotDensityFillSymbol dotfillSymbol = new DotDensityFillSymbol();
            dotfillSymbol.Color = color;
            dotfillSymbol.DotSize = width;
            dotfillSymbol.DotSpacing = 15;
            dotfillSymbol.Outline = lineSymbol;

            IFillSymbol fillSymbol = new SimpleFillSymbol();
            fillSymbol.Outline = lineSymbol;
            fillSymbol.Outline.Width = width;
            fillSymbol.Color = color;
            fillSymbol.Color.Transparency = 100;//透明填充

            IScreenDisplay2 screenDisplay = activeView.ScreenDisplay as IScreenDisplay2;
            screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
            switch (geometry.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    IMarkerSymbol markerSymbol = new SimpleMarkerSymbol();
                    markerSymbol.Color = color;
                    markerSymbol.Size = width;
                    screenDisplay.SetSymbol(markerSymbol as ISymbol);
                    screenDisplay.DrawPoint(geometry);
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    screenDisplay.SetSymbol(lineSymbol as ISymbol);
                    screenDisplay.DrawPolyline(geometry);
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    screenDisplay.SetSymbol(fillSymbol as ISymbol);
                    dotfillSymbol.BackgroundColor = new RgbColor();
                    screenDisplay.DrawPolygon(geometry);
                    break;
                case esriGeometryType.esriGeometryEnvelope:
                    screenDisplay.SetSymbol(fillSymbol as ISymbol);
                    screenDisplay.DrawRectangle(geometry as IEnvelope);
                    break;
            }
            screenDisplay.FinishDrawing();
        }

        /// <summary>
        /// 在地图上添加一个点对象，返回添加后的IElement对象
        /// </summary>
        /// <param name="point">点对象</param>
        /// <param name="activeView">地图对象，可以直接有IMap转换，IMap as IActiveView</param>
        /// <param name="rgb">点的颜色</param>
        /// <param name="size">点的对象</param>
        /// <returns></returns>
        public static IElement AddMarker(IPoint point, IActiveView activeView, IRgbColor rgb = null, double size = 5)
        {
            if (rgb == null)
            {
                rgb = new RgbColor()
                {
                    Red = 255,
                    Blue = 255,
                    Green = 0,
                    Transparency = 10
                };
            }   
            //设置大小、颜色、风格、旋转角度
            ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol();
            markerSymbol.Size = size;
            markerSymbol.Color = rgb;

            IMarkerElement markerElement = new MarkerElement() as IMarkerElement;
            markerElement.Symbol = markerSymbol;
            
            IElement element = (IElement)markerElement;
            element.Geometry = point;
            activeView.GraphicsContainer.AddElement(element, 0);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, activeView.Extent);
            return element;
        }

        /// <summary>
        /// 在地图上添加一个多边形对象，返回添加后的IElement对象。
        /// </summary>
        /// <param name="polygon">多边形对象</param>
        /// <param name="activeView">地图对象，可以直接有IMap转换，IMap as IActiveView</param>
        /// <param name="rgbLine">面的颜色</param>
        /// <param name="lineWidth">边线的宽度</param>
        /// <param name="rgbFill">填充颜色</param>
        /// <param name="filltransparent">0透明，255不透明</param>
        /// <returns></returns>
        public static IElement AddPolygon(IPolygon polygon, IActiveView activeView, IRgbColor rgbLine = null, double lineWidth = 1, IRgbColor rgbFill = null, byte filltransparent = 0)
        {
            ILineSymbol plineSymbol = new SimpleLineSymbol();
            if (rgbLine == null)
            {
                rgbLine = new RgbColor()
                {
                    Red = 255,
                    Blue = 255,
                    Green = 0,
                    Transparency = 10
                };
            }
            plineSymbol.Color = rgbLine;
            plineSymbol.Width = lineWidth;

            IFillShapeElement elementPolygon = new PolygonElement() as IFillShapeElement;
            IElement element = (IElement)elementPolygon;
            element.Geometry = polygon;
            IFillSymbol polySymbol = new SimpleFillSymbol();
            if (rgbFill == null)
            {
                rgbFill = new RgbColor()
                {
                    Red = 0,
                    Blue = 0,
                    Green = 0,
                    Transparency = filltransparent
                };
            }
            else
            {
                rgbFill.Transparency = filltransparent;
            }
            polySymbol.Color = rgbFill;
            polySymbol.Outline = plineSymbol;
            elementPolygon.Symbol = polySymbol;
            activeView.GraphicsContainer.AddElement(element, 0);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, activeView.Extent);
            return element;
        }

        /// <summary>
        /// 在地图上添加一个多边形对象，返回添加后的IElement对象。
        /// </summary>
        /// <param name="polygon">多边形对象</param>
        /// <param name="activeView">地图对象，可以直接有IMap转换，IMap as IActiveView</param>
        /// <param name="r">边线Red的值</param>
        /// <param name="g">边线Green的值</param>
        /// <param name="b">边线Blue的值</param>
        /// <param name="lineWidth"></param>
        /// <param name="filltransparent"></param>
        /// <returns></returns>
        public static IElement AddPolygon(IPolygon polygon, IActiveView activeView, int r,int g, int b, double lineWidth = 1, byte filltransparent = 0)
        {
            IRgbColor rgb = new RgbColor()
            {
                Red = r,
                Green = g,
                Blue = b
            };            
            return AddPolygon(polygon, activeView, rgb, lineWidth, rgb, filltransparent);
        }

        /// <summary>
        /// 在地图上添加一个线对象，返回添加后的IElement对象
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">终点y坐标</param>
        /// <param name="activeView">地图或布局视图</param>
        /// <param name="r">红</param>
        /// <param name="g">绿</param>
        /// <param name="b">蓝</param>
        /// <param name="width">宽度</param>
        /// <returns></returns>
        public static IElement AddLine(double X1, double Y1, double X2, double Y2, IActiveView activeView, int r, int g, int b, double width = 1)
        {
            IRgbColor tmpColor = new RgbColor();
            tmpColor.Red = r;
            tmpColor.Green = g;
            tmpColor.Blue = b;
            return AddLine(X1, Y1, X2, Y2,activeView, tmpColor, width );
        }

        /// <summary>
        /// 在地图上添加一个线对象，返回添加后的IElement对象
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">终点y坐标</param>
        /// <param name="activeView">地图或布局视图</param>
        /// <param name="color">线的颜色</param>
        /// <param name="width">宽度</param>
        /// <returns></returns>
        public static IElement AddLine(double X1, double Y1, double X2, double Y2, IActiveView activeView, IRgbColor color=null, double width=1)
        {
            object missing = Type.Missing;
            IPolyline polyLine = new Polyline() as IPolyline;
            IPointCollection ptcol = polyLine as IPointCollection;
            ptcol.AddPoint(new Point() { X = X1, Y = Y1 }, ref missing, ref missing);
            ptcol.AddPoint(new Point() { X = X2, Y = Y2 }, ref missing, ref missing);
            return AddLine(polyLine, activeView, color, width);
        }

        /// <summary>
        /// 在地图上添加一个线对象，返回添加后的IElement对象
        /// </summary>
        /// <param name="line">线对象</param>
        /// <param name="activeView">地图对象，可以直接有IMap转换，IMap as IActiveView</param>
        /// <param name="r">线Red的值</param>
        /// <param name="g">线Green的值</param>
        /// <param name="b">线Blue的值</param>
        /// <param name="width">线的宽度</param>
        /// <returns></returns>
        public static IElement AddLine(IPolyline line, IActiveView activeView, int r, int g, int b, double width = 1)
        {
            IRgbColor rgb = new RgbColor()
            {
                Red = r,
                Green = g,
                Blue = b
            };
            return AddLine(line, activeView, rgb, width);
        }

        /// <summary>
        /// 在地图上添加一个线对象，返回添加后的IElement对象
        /// </summary>
        /// <param name="line">线对象</param>
        /// <param name="activeView">地图对象，可以直接有IMap转换，IMap as IActiveView</param>
        /// <param name="rgbLine">面的颜色</param>
        /// <param name="width">线的宽度</param>
        /// <returns></returns>
        public static IElement AddLine(IPolyline line, IActiveView activeView, IRgbColor rgbLine = null, double width = 1)
        {
            if (line == null || line.IsEmpty) return null;
            ILineSymbol plineSymbol = new SimpleLineSymbol();
            if (rgbLine == null)
            {
                rgbLine = new RgbColor()
                {
                    Red = 0,
                    Blue = 0,
                    Green = 0,
                    Transparency = 255
                };
            }   
            plineSymbol.Color = rgbLine;
            plineSymbol.Width = width;
            IElement ele = new LineElement();
            ele.Geometry = line;
            ILineElement e = ele as ILineElement;
            e.Symbol = plineSymbol;
            activeView.GraphicsContainer.AddElement(ele, 1);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, ele, activeView.Extent);
            return ele;
        }

        /// <summary>
        /// 重载需要添加Z值的线element
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">重点y坐标</param>
        /// <param name="r">红</param>
        /// <param name="g">绿</param>
        /// <param name="b">蓝</param>
        /// <param name="width">宽度</param>
        /// <param name="activeView">地图或布局视图</param>
        /// <param name="z">高程值</param>
        public static IElement AddLine(double X1, double Y1, double X2, double Y2, IActiveView activeView, int r, int g, int b, double width, double z)
        {
            IPoint p1 = new Point();
            IPoint p2 = new Point();
            p1.PutCoords(X1, Y1);
            p2.PutCoords(X2, Y2);
            p1.Z = z;
            p2.Z = z;
            object missing = Type.Missing;
            IPolyline line = new Polyline() as IPolyline;
            IPointCollection pts = line as IPointCollection;
            pts.AddPoint(p1, ref missing, ref missing);
            pts.AddPoint(p2, ref missing, ref missing);
            return AddLine(line, activeView, r, g, b, width);            
        }

        /// <summary>
        /// 添加文本到布局控件中
        /// </summary>
        /// <param name="activeView">地图或布局视图</param>
        /// <param name="X1">X坐标</param>
        /// <param name="Y1">Y坐标</param>
        /// <param name="text">文本</param>
        /// <param name="HAlignment">水平位置</param>
        /// <param name="VAlignment">垂直位置</param>
        /// <param name="fontName">字体</param>
        /// <param name="isbold">是否加粗</param>
        /// <param name="decSize">字体大小</param>
        /// <param name="angle">旋转角度</param>
        public static IElement AddText(IActiveView activeView, double X1, double Y1, string text, decimal decSize, esriTextHorizontalAlignment HAlignment = esriTextHorizontalAlignment.esriTHALeft, esriTextVerticalAlignment VAlignment = esriTextVerticalAlignment.esriTVATop, string fontName = "宋体", bool isbold = false, double angle = 0)
        {
            IPoint p = new Point();
            p.PutCoords(X1, Y1);
            IElement ele = new TextElement();
            ele.Geometry = p;
            StdFont pFont = new StdFont();
            pFont.Name = fontName;
            pFont.Bold = isbold;
            pFont.Size = decSize;
            IFormattedTextSymbol symbol = new TextSymbol();
            symbol.Font = (IFontDisp)pFont;
            symbol.HorizontalAlignment = HAlignment;
            symbol.VerticalAlignment = VAlignment;
            symbol.FlipAngle = angle;

            ITextElement txtEle = ele as ITextElement;
            txtEle.Symbol = symbol;
            txtEle.Text = text;
            activeView.GraphicsContainer.AddElement(ele, 1);
            return ele;
        }

        #endregion

        #region 创建Element对象
        /// <summary>
        /// 创建点Element对象
        /// </summary>
        /// <param name="point">点对象</param>
        /// <param name="rgb">点的颜色</param>
        /// <param name="size">点的对象</param>
        /// <param name="name">对象名称</param>
        /// <param name="property">对象扩展属性</param>
        /// <returns></returns>
        public static IElement CreateMarker(IPoint point, IRgbColor rgb = null, double size = 5,string name ="", dynamic property = null)
        {
            IMarkerElement markerEle = new MarkerElement() as IMarkerElement;
            markerEle.Symbol = CreateSimpleMarkerSymbol(rgb, size);
            IElement element = (IElement)markerEle;
            element.Geometry = point;
            if (string.IsNullOrWhiteSpace(name)) name = DateTime.Now.ToString("yyyyMMddHHmmssttt");
            element.SetCustomProperty(name, (object)property);
            return element;
        }

        /// <summary>
        /// 创建面Element对象
        /// </summary>
        /// <param name="polygon">多边形对象</param>
        /// <param name="rgbLine">面的颜色</param>
        /// <param name="lineWidth">边线的宽度</param>
        /// <param name="rgbFill">填充颜色</param>
        /// <param name="filltransparent">0透明，255不透明</param>
        /// <param name="name">对象名称</param>
        /// <param name="property">对象扩展属性</param>
        /// <returns></returns>
        public static IElement CreatePolygon(IPolygon polygon, IRgbColor rgbLine = null, double lineWidth = 1, IRgbColor rgbFill = null, byte filltransparent = 0, string name = "", dynamic property = null)
        {
            if (rgbLine == null)
            {
                rgbLine = new RgbColor()
                {
                    Red = 255,
                    Blue = 255,
                    Green = 0,
                    Transparency = 10
                };
            }
            ILineSymbol lineSymbol = new SimpleLineSymbol();            
            lineSymbol.Color = rgbLine;
            lineSymbol.Width = lineWidth;
            IFillSymbol polySymbol = new SimpleFillSymbol();
            if (rgbFill == null)
            {
                rgbFill = new RgbColor()
                {
                    Red = 0,
                    Blue = 0,
                    Green = 0
                };
            }
            rgbFill.Transparency = filltransparent;
            polySymbol.Color = rgbFill;
            polySymbol.Outline = lineSymbol;

            IFillShapeElement shapeElem = new PolygonElement() as IFillShapeElement;
            shapeElem.Symbol = polySymbol;
            IElement element = (IElement)shapeElem;
            element.Geometry = polygon;
            element.SetCustomProperty(name, (object)property);
            return element;
        }

        /// <summary>
        /// 创建面Element对象
        /// </summary>
        /// <param name="polygon">多边形对象</param>
        /// <param name="r">边线Red的值</param>
        /// <param name="g">边线Green的值</param>
        /// <param name="b">边线Blue的值</param>
        /// <param name="lineWidth"></param>
        /// <param name="filltransparent"></param>
        /// <returns></returns>
        public static IElement CreatePolygon(IPolygon polygon, int r, int g, int b, double lineWidth = 1, byte filltransparent = 0)
        {
            IRgbColor rgb = new RgbColor()
            {
                Red = r,
                Green = g,
                Blue = b
            };
            return CreatePolygon(polygon, rgb, lineWidth, rgb, filltransparent);
        }

        /// <summary>
        /// 获得线Element对象
        /// </summary>
        /// <param name="width">线的宽度</param>
        /// <returns></returns>
        [Obsolete]
        public static ILineElement CreateLine(double width)
        {
            IRgbColor pColor = new RgbColor();
            pColor.Red = 255;
            pColor.Green = 255;
            pColor.Blue = 0;
            ISimpleLineSymbol pLineSymbol = new SimpleLineSymbol();
            pLineSymbol.Width = width;
            pLineSymbol.Color = pColor;
            ILineElement pLineElement = new LineElement() as ILineElement;
            pLineElement.Symbol = pLineSymbol;
            return pLineElement;
        }

        /// <summary>
        /// 创建线Element对象
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">终点y坐标</param>
        /// <param name="r">红</param>
        /// <param name="g">绿</param>
        /// <param name="b">蓝</param>
        /// <param name="width">宽度</param>
        /// <returns></returns>
        [Obsolete]
        public static IElement CreateLine(double X1, double Y1, double X2, double Y2, int r, int g, int b, double width = 1)
        {
            IRgbColor tmpColor = new RgbColor()
            {
                Red = r,
                Green = g,
                Blue = b
            };
            return CreateLine(X1, Y1, X2, Y2, tmpColor, width);
        }

        ///// <summary>
        ///// 创建线Element对象
        ///// </summary>
        ///// <param name="line">线对象</param>
        ///// <param name="r">线Red的值</param>
        ///// <param name="g">线Green的值</param>
        ///// <param name="b">线Blue的值</param>
        ///// <param name="width">线的宽度</param>
        ///// <returns></returns>
        //[Obsolete]
        //public static IElement CreateLine(IPolyline line, int r, int g, int b, double width = 1)
        //{
        //    IRgbColor rgb = new RgbColor()
        //    {
        //        Red = r,
        //        Green = g,
        //        Blue = b
        //    };
        //    return CreateLine(line, rgb, width);
        //}

        /// <summary>
        /// 创建线Element对象
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">终点y坐标</param>
        /// <param name="color">线的颜色: new RgbColor(){ Red = r,Green = g,Blue = b}</param>
        /// <param name="width">宽度</param>
        /// <returns></returns>        
        public static IElement CreateLine(double X1, double Y1, double X2, double Y2, IRgbColor color = null, double width = 1)
        {
            object missing = Type.Missing;
            IPolyline polyLine = new Polyline() as IPolyline;
            IPointCollection ptcol = polyLine as IPointCollection;
            ptcol.AddPoint(new Point() { X = X1, Y = Y1 }, ref missing, ref missing);
            ptcol.AddPoint(new Point() { X = X2, Y = Y2 }, ref missing, ref missing);
            return CreateLine(polyLine, color, width);
        }

        /// <summary>
        /// 创建线Element对象
        /// </summary>
        /// <param name="X1">起点x坐标</param>
        /// <param name="Y1">起点Y坐标</param>
        /// <param name="X2">终点x坐标</param>
        /// <param name="Y2">重点y坐标</param>
        /// <param name="r">红</param>
        /// <param name="g">绿</param>
        /// <param name="b">蓝</param>
        /// <param name="width">宽度</param>
        /// <param name="z">高程值，Z值相同</param>
        /// <param name="hasz">是否有Z值</param>
        public static IElement CreateLine(double X1, double Y1, double X2, double Y2, int r, int g, int b, double width, double z = 0, bool hasz = false)
        {
            IPoint p1 = new Point() { X = X1, Y = Y1 };
            IPoint p2 = new Point() { X = X2, Y = Y2 };
            if (hasz)
            {
                p1.Z = z;
                p2.Z = z;
            }
            object missing = Type.Missing;
            IPolyline line = new Polyline() as IPolyline;
            IPointCollection pts = line as IPointCollection;
            pts.AddPoint(p1, ref missing, ref missing);
            pts.AddPoint(p2, ref missing, ref missing);
            IRgbColor rgb = new RgbColor()
            {
                Red = r,
                Green = g,
                Blue = b
            };
            return CreateLine(line, rgb, width);
        }

        /// <summary>
        /// 创建线Element对象
        /// </summary>
        /// <param name="line">线对象</param>
        /// <param name="rgbLine">面的颜色: new RgbColor(){ Red = r,Green = g,Blue = b}</param>
        /// <param name="width">线的宽度</param>
        /// <returns></returns>
        public static IElement CreateLine(IPolyline line, IRgbColor rgbLine = null, double width = 1)
        {
            if (rgbLine == null)
            {
                rgbLine = new RgbColor()
                {
                    Red = 0,
                    Blue = 0,
                    Green = 0,
                    Transparency = 255
                };
            }
            ILineSymbol lineSymbol = new SimpleLineSymbol();            
            lineSymbol.Color = rgbLine;
            lineSymbol.Width = width;

            IElement ele = new LineElement();
            ele.Geometry = line;
            ILineElement e = ele as ILineElement;
            e.Symbol = lineSymbol;
            return ele;
        }

        /// <summary>
        /// 创建文本Element对象
        /// </summary>
        /// <param name="X1">X坐标</param>
        /// <param name="Y1">Y坐标</param>
        /// <param name="text">文本</param>
        /// <param name="decSize">字体大小</param>
        /// <param name="HAlignment">水平位置</param>
        /// <param name="VAlignment">垂直位置</param>
        /// <param name="fontName">字体</param>
        /// <param name="isbold">是否加粗</param>
        /// <param name="angle">旋转角度</param>
        public static IElement CreateText(double X1, double Y1, string text, decimal decSize, esriTextHorizontalAlignment HAlignment = esriTextHorizontalAlignment.esriTHALeft, esriTextVerticalAlignment VAlignment = esriTextVerticalAlignment.esriTVATop, string fontName = "宋体", bool isbold = false, double angle = 0)
        {
            IPoint p = new Point();
            p.PutCoords(X1, Y1);
            IElement ele = new TextElement();
            ele.Geometry = p;
            StdFont pFont = new StdFont();
            pFont.Name = fontName;
            pFont.Bold = isbold;
            pFont.Size = decSize;
            IFormattedTextSymbol symbol = new TextSymbol();
            symbol.Font = (IFontDisp)pFont;
            symbol.HorizontalAlignment = HAlignment;
            symbol.VerticalAlignment = VAlignment;
            symbol.FlipAngle = angle;
            
            ITextElement txtEle = ele as ITextElement;
            txtEle.Symbol = symbol;
            txtEle.Text = text;
            return ele;
        }
        #endregion
        
        /// <summary>
        /// 绘制Geometry，同时返回生成的IElement
        /// </summary>
        /// <param name="control"></param>
        /// <param name="flash"></param>
        /// <returns></returns>
        public static IElement DrawGeometry(IMapControl3 control, IGeometry flash)
        {
            IRgbColor rgbline = new RgbColor()
            {
                Red = 255,
                Blue = 0,
                Green = 0,
                Transparency = 255
            };
            IElement returnEle = null;
            if (flash is IPoint)
            {
                returnEle = AddMarker(flash as IPoint, control.ActiveView);
            }
            else if (flash is IPolygon)
            {
                
                returnEle = AddPolygon(flash as IPolygon, control.ActiveView, rgbline, 1);
            }
            else if (flash is IPolyline)
            {
                returnEle = AddLine(flash as IPolyline, control.ActiveView, rgbline, 2);
            }
            return returnEle;
        }

        /// <summary>
        /// 移动图幅周边的要素
        /// </summary>
        /// <param name="element"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void MovePageLyrOutlbl(IElement element, double x, double y)
        {
            if (element.Geometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                IPoint pPoint = (IPoint)element.Geometry;
                pPoint.X = pPoint.X + x;
                pPoint.Y = pPoint.Y + y;
                element.Geometry = pPoint;
            }
            else
            {
                IEnvelope envelope = element.Geometry.Envelope;
                envelope.Offset(x, y);
                element.Geometry = envelope;
            }
        }
                
        /// <summary>
        /// 改变要素的字内容
        /// </summary>
        /// <param name="textElement"></param>
        /// <param name="text"></param>
        public static void ReWriteElementText(ITextElement textElement, string text)
        {
            if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text ))
            {
                return;
            }
            else
            {
                textElement.Text = text;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dblS"></param>
        /// <param name="textElement"></param>
        /// <param name="blnEnter"></param>
        public static void ReWriteCoor(double dblS, ref ITextElement textElement, bool blnEnter)
        {
            int lngD = Convert.ToInt32(Math.Truncate(dblS / 3600));
            int lngM = Convert.ToInt32(Math.Truncate((dblS - lngD * 3600) / 60));
            int lngS = Convert.ToInt32(dblS - lngD * 3600 - lngM * 60);
            string strM = lngM.ToString("0#");
            string strS = lngS.ToString("0#");

            if (blnEnter == true)
            {
                textElement.Text = lngD.ToString() + "°" + "\r\n" + strM + "'" + strS + "\"";
            }
            else
            {
                textElement.Text = lngD.ToString() + "°" + strM + "'" + strS + "\"";
            }
        }

        #region Element

        /// <summary>
        /// 在地图上画Element
        /// </summary>
        /// <param name="element">Element</param>
        /// <param name="map">地图控件</param>
        public static void AddElement(IElement element, IMap map)
        {
            IActiveView activeView = (IActiveView)map;
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)map;

            pGraphicsContainer.AddElement(element, 10);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
        }

        /// <summary>
        /// 在地图上画Element
        /// </summary>
        /// <param name="element">Element</param>
        /// <param name="mapControl">地图控件</param>
        public static void AddElement(IElement element, IMapControl3 mapControl)
        {
            IMap map = mapControl.Map;
            IActiveView activeView = (IActiveView)map;
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)map;
            pGraphicsContainer.AddElement(element, 10);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
        }

        
        

        /// <summary>
        /// 添加文本
        /// </summary>
        /// <param name="X1"></param>
        /// <param name="Y1"></param>
        /// <param name="sText"></param>
        /// <param name="HAlignment"></param>
        /// <param name="VAlignment"></param>
        /// <param name="strFontName"></param>
        /// <param name="isbold"></param>
        /// <param name="decSize"></param>
        /// <param name="Angle"></param>
        /// <returns></returns>
        public static IElement GetTextElement(double X1, double Y1, string sText, int HAlignment, int VAlignment, string strFontName, bool isbold, decimal decSize, Double Angle)
        {
            IPoint pP1New = new Point();
            pP1New.PutCoords(X1, Y1);

            StdFont pFont = new StdFont();
            pFont.Name = strFontName;
            pFont.Bold = isbold;
            pFont.Size = decSize;

            ITextElement pTextEle = (ITextElement)new TextElement();
            pTextEle.Text = sText;

            IFormattedTextSymbol pTextSymbol = new TextSymbol();
            pTextSymbol.Font = (IFontDisp)pFont;
            pTextSymbol.HorizontalAlignment = (esriTextHorizontalAlignment)HAlignment;
            pTextSymbol.VerticalAlignment = (esriTextVerticalAlignment)VAlignment;
            pTextSymbol.FlipAngle = Angle;

            pTextEle.Symbol = pTextSymbol;

            IElement pElement = (IElement)pTextEle;
            pElement.Geometry = pP1New;

            return pElement;
        }

        /// <summary>
        /// 添加圆
        /// </summary>
        /// <param name="pCPoint">圆心</param>
        /// <param name="Radius">半径</param>
        /// <param name="IsCCW">是否逆时针</param>
        /// <param name="PageLayoutControl1">打印视图</param>
        public static void addCircleEle(IPoint pCPoint, double Radius, bool IsCCW, IPageLayoutControl2 PageLayoutControl1)
        {
            ICircularArc pCircle = new CircularArc();
            IConstructCircularArc pConstCArc = (IConstructCircularArc)new CircularArc();
            pConstCArc.ConstructCircle(pCPoint, Radius, IsCCW);
            pCircle = (ICircularArc)pConstCArc;
            IPoint pFP = pCircle.FromPoint;
            pFP.Z = pCPoint.Z;
            pCircle.FromPoint = pFP;
            IPolygon pPolygon = (IPolygon)new Polygon();
            ISegmentCollection pSegmentCollection = (ISegmentCollection)pPolygon;
            object Missing = Type.Missing;
            pSegmentCollection.AddSegment((ISegment)pCircle, ref Missing, ref Missing);
            PageLayoutControl1.AddElement(new CircleElement(), pPolygon, Missing, Missing, 0);
        }

        /// <summary>
        /// 添加圆
        /// </summary>
        /// <param name="pCPoint"></param>
        /// <param name="Radius"></param>
        /// <param name="IsCCW"></param>
        /// <returns></returns>
        public static IElement getCircleEle(IPoint pCPoint, double Radius, bool IsCCW)
        {
            ICircularArc pCircle = new CircularArc();
            IConstructCircularArc pConstCArc = (IConstructCircularArc)new CircularArc();
            pConstCArc.ConstructCircle(pCPoint, Radius, IsCCW);
            pCircle = (ICircularArc)pConstCArc;
            IPoint pFP = pCircle.FromPoint;
            pFP.Z = pCPoint.Z;
            pCircle.FromPoint = pFP;
            IPolygon pPolygon = (IPolygon)new Polygon();
            ISegmentCollection pSegmentCollection = (ISegmentCollection)pPolygon;
            object Missing = Type.Missing;
            pSegmentCollection.AddSegment((ISegment)pCircle, ref Missing, ref Missing);

            IElement pElement = (IElement)new CircleElement();
            pElement.Geometry = pPolygon;

            return pElement;
        }

        /// <summary>
        /// 添加圆
        /// </summary>
        /// <param name="p_AV"></param>
        /// <param name="pElement"></param>
        /// <returns></returns>
        public static IElement AddCircurElement(IActiveView p_AV, ref IElement pElement)
        {
            IRgbColor pRGBColor = new RgbColor();
            pRGBColor.Red = 0;
            pRGBColor.Green = 255;
            pRGBColor.Blue = 0;
            pRGBColor.Transparency = 150;
            IColor pColor = pRGBColor as IColor;

            IRgbColor pLineColor = new RgbColor();
            pLineColor.Red = 0;
            pLineColor.Green = 0;
            pLineColor.Blue = 0;
            pLineColor.Transparency = 255;
            IColor LineCl = pLineColor as IColor;

            //set outline
            ILineSymbol pLineSymbol = new SimpleLineSymbol();
            pLineSymbol.Color = LineCl;
            pLineSymbol.Width = 1;
            //set symbol
            IFillSymbol pFillSymbol = new SimpleFillSymbol();
            pFillSymbol.Color = pColor;
            pFillSymbol.Outline = pLineSymbol;
            //set fillshape
            IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
            if (pFillShapeElement != null) pFillShapeElement.Symbol = pFillSymbol;
            else return null;
            return pFillShapeElement as IElement;

        }

        /// <summary>
        /// 添加element
        /// </summary>
        /// <param name="pEle"></param>
        /// <param name="pGeo"></param>
        /// <param name="m_pGraCont"></param>
        public static void addElement(IElement pEle, IGeometry pGeo, IGraphicsContainer m_pGraCont)
        {
            pEle.Geometry = pGeo;
            m_pGraCont.AddElement(pEle, 0);
        }

        

        /// <summary>
        /// 将点、线、面图形分别转换成相应的Element
        /// </summary>
        /// <param name="pGeometry"></param>
        /// <returns></returns>
        public static IElement GetEleFromGeom(IGeometry pGeometry)
        {
            IElement pElement = null;
            IRgbColor pRgbColor;
            pRgbColor = new RgbColor();
            pRgbColor.Red = 255;
            pRgbColor.Blue = 0;
            pRgbColor.Green = 0;

            switch (pGeometry.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    IMarkerElement pMarkerElement = new MarkerElement() as IMarkerElement;

                    ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();
                    pSimpleMarkerSymbol.Size = 5;
                    pSimpleMarkerSymbol.Color = pRgbColor;
                    pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
                    pSimpleMarkerSymbol.Outline = true;
                    pSimpleMarkerSymbol.OutlineColor = pRgbColor;
                    pSimpleMarkerSymbol.OutlineSize = 1;
                    pMarkerElement.Symbol = pSimpleMarkerSymbol;
                    pElement = (IElement)pMarkerElement;
                    break;

                case esriGeometryType.esriGeometryPolyline:
                    ILineElement pLineElement = new LineElement() as ILineElement;

                    ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
                    pSimpleLineSymbol.Width = 1;
                    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDash;
                    pSimpleLineSymbol.Color = pRgbColor;

                    pLineElement.Symbol = pSimpleLineSymbol;
                    pElement = (IElement)pLineElement;
                    break;

                case esriGeometryType.esriGeometryPolygon:
                    IFillShapeElement pFillShapeElement = new PolygonElement() as IFillShapeElement;

                    ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
                    pSimpleFillSymbol.Color = pRgbColor;
                    pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;

                    pFillShapeElement.Symbol = pSimpleFillSymbol;
                    pElement = (IElement)pFillShapeElement;
                    break;

                case esriGeometryType.esriGeometryBag:
                    IGeometryCollection pGeomCln = pGeometry as IGeometryCollection;
                    break;

                default:
                    pGeometry = Convertor.ConvertLow2High(pGeometry);

                    if (pGeometry != null) pElement = GetEleFromGeom(pGeometry);

                    break;
            }


            if (pElement != null) pElement.Geometry = pGeometry;
            return pElement;
        }

        /// <summary>
        /// 将点、线、面图形分别转换成相应的Element
        /// </summary>
        /// <param name="pGeometry"></param>
        /// <param name="pRgbColor"></param>
        /// <returns></returns>
        public static IElement GetEleFromGeomAndColor(IGeometry pGeometry, IRgbColor pRgbColor)
        {
            IElement pElement = null;

            switch (pGeometry.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    IMarkerElement pMarkerElement = new MarkerElement() as IMarkerElement;

                    ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();
                    pSimpleMarkerSymbol.Size = 5;
                    pSimpleMarkerSymbol.Color = pRgbColor;

                    pMarkerElement.Symbol = pSimpleMarkerSymbol;
                    pElement = (IElement)pMarkerElement;
                    break;

                case esriGeometryType.esriGeometryPolyline:
                    ILineElement pLineElement = new LineElement() as ILineElement;

                    ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
                    pSimpleLineSymbol.Width = 3;
                    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                    pSimpleLineSymbol.Color = pRgbColor;

                    pLineElement.Symbol = pSimpleLineSymbol;
                    pElement = (IElement)pLineElement;
                    break;

                case esriGeometryType.esriGeometryPolygon:
                    IFillShapeElement pFillShapeElement = new PolygonElement() as IFillShapeElement;

                    ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
                    pSimpleFillSymbol.Color = pRgbColor;
                    pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;

                    pFillShapeElement.Symbol = pSimpleFillSymbol;
                    pElement = (IElement)pFillShapeElement;
                    break;

                default:
                    pGeometry = Convertor.ConvertLow2High(pGeometry);

                    if (pGeometry != null) pElement = GetEleFromGeom(pGeometry);

                    break;
            }

            if (pElement != null) pElement.Geometry = pGeometry;
            return pElement;
        }
        
        /// <summary>
        /// 绘制point
        /// </summary>
        /// <param name="point"></param>
        /// <param name="activeView"></param>
        public static void DrawPointXOR(IGeometry point, IActiveView activeView)
        {
            //IFillSymbol pFillSymbol = new SimpleFillSymbol();
            ISimpleMarkerSymbol fillSymbol = new SimpleMarkerSymbol();
            ISymbol symbol = (ISymbol)fillSymbol;
            symbol.ROP2 = esriRasterOpCode.esriROPMaskNotPen;
            //填充色
            IRgbColor pRGBColor = new RgbColor();
            pRGBColor.UseWindowsDithering = false;
            pRGBColor.Red = 0;
            pRGBColor.Green = 255;
            pRGBColor.Blue = 0;
            pRGBColor.Transparency = 255;
            fillSymbol.Color = pRGBColor;
            fillSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;

            pRGBColor.Red = 0;
            pRGBColor.Green = 0;
            pRGBColor.Blue = 0;

            fillSymbol.Outline = true;
            fillSymbol.OutlineSize = 1.1;
            fillSymbol.OutlineColor = pRGBColor;
            fillSymbol.Size = 1;


            //外围线型
            //ILineSymbol pLineSymbol = pFillSymbol.Outline;
            //ISymbol pLSymbol = (ISymbol)pLineSymbol;
            //pLSymbol.ROP2 = esriRasterOpCode.esriROPMaskNotPen;
            //外围颜色
            //pRGBColor.UseWindowsDithering = false;
            //pRGBColor.Red = 255;
            //pRGBColor.Green = 255;
            //pRGBColor.Blue = 255;
            //pRGBColor.Transparency = 255;
            //pLineSymbol.Color = pRGBColor;
            //pLineSymbol.Width = 0.1;
            //pFillSymbol.Outline = pLineSymbol;

            activeView.ScreenDisplay.StartDrawing(0, (short)esriScreenCache.esriNoScreenCache);
            activeView.ScreenDisplay.SetSymbol(symbol);
            activeView.ScreenDisplay.DrawPoint(point);
            activeView.ScreenDisplay.FinishDrawing();

            symbol = null;
            fillSymbol = null;
            //pLSymbol = null;
            //pLineSymbol = null;
            pRGBColor = null;
        }
        
        /// <summary>
        /// 获取地图控件帧的范围
        /// </summary>
        /// <param name="pGraphicsContainer">图形容器</param>
        /// <returns>帧的范围</returns>
        public static IEnvelope GetMapFrameSize(IGraphicsContainer pGraphicsContainer)
        {
            pGraphicsContainer.Reset();
            IElement pElememt = pGraphicsContainer.Next();
            IGeometry pGeometry = null;
            while (pElememt != null)
            {
                if (pElememt is IMapFrame)
                {
                    IMapFrame pMapFrame = (IMapFrame)pElememt;
                    //得到MapFrame的矩形框
                    pGeometry = pElememt.Geometry;
                    break;
                }
                pElememt = pGraphicsContainer.Next();
            }
            if (pGeometry != null)
                return pGeometry.Envelope;
            else
                return null;
        }
        #endregion

        #region 文本
        /// <summary>
        /// 文本样式
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="color">颜色</param>
        /// <param name="size">大小</param>
        /// <returns></returns>
        public static ITextSymbol GetTextSymbol(string text, IColor color, double size)
        {
            ITextSymbol pTextSym = new TextSymbol();
            pTextSym.Text = text;
            pTextSym.Color = color;
            pTextSym.Size = size;
            pTextSym.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
            pTextSym.Angle = 0;
            pTextSym.VerticalAlignment = esriTextVerticalAlignment.esriTVABaseline;
            return pTextSym;
        }
        /// <summary>
        /// 获取文本的样式
        /// </summary>
        /// <param name="text">文本内容</param>
        /// <param name="color">颜色</param>
        /// <param name="size">文本大小</param>
        /// <param name="HAlign">水平对齐方式</param>
        /// <param name="VAlign">垂直对齐方式</param>
        /// <param name="angle">文本角度</param>
        /// <param name="fontname">字体名称: 宋体</param>
        /// <param name="rightToLeft">从右到左</param>
        /// <returns></returns>
        public static ITextSymbol GetTextSymbol(string text, IColor color, double size, esriTextHorizontalAlignment HAlign, esriTextVerticalAlignment VAlign, double angle, string fontname, bool rightToLeft)
        {
            ITextSymbol pTextSym = new TextSymbolClass();
            pTextSym.Text = text;
            pTextSym.Color = color;
            pTextSym.Size = size;
            pTextSym.HorizontalAlignment = HAlign;
            pTextSym.Angle = angle;
            pTextSym.VerticalAlignment = VAlign;
            pTextSym.RightToLeft = rightToLeft;
            if (!string.IsNullOrWhiteSpace(fontname))
            {
                try
                {
                    pTextSym.Font.Name = fontname;
                }
                catch
                {
                    //字体设置有误时取默认的字体
                }
            }
            return pTextSym;
        }
        /// <summary>
        /// 绘制文本
        /// </summary>
        /// <param name="geo">文本所在的位置</param>
        /// <param name="text">文本的值</param>   
        /// <param name="isscale">文本随地图缩放</param>
        /// <param name="symbol">文本的风格</param>
        /// <returns>创建的文本对象IElement</returns>
        public static IElement GetTextElement(IGeometry geo, string text, bool isscale, ITextSymbol symbol)
        {
            if (geo == null) return null;
            ITextElement textElement = new TextElement() as ITextElement;
            textElement.Text = text;
            textElement.ScaleText = isscale;
            if (symbol != null)
                textElement.Symbol = symbol;
            IElement element = textElement as IElement;
            element.Geometry = geo;
            return element;
        }
        /// <summary>
        /// 绘制文本
        /// </summary>
        /// <param name="geo">文本所在的位置</param>
        /// <param name="text">文本的值</param>
        /// <param name="isscale"></param>
        /// <param name="elementName">对象的名称</param>
        /// <param name="elementProperty">对象的属性</param>
        /// <param name="symbol">文本的风格</param>
        /// <returns>创建的文本对象IElement</returns>
        public static IElement GetTextElement(IGeometry geo, string text, bool isscale, string elementName, object elementProperty, ITextSymbol symbol)
        {
            if (geo == null) return null;
            ITextElement textElement = new TextElementClass();
            textElement.Text = text;
            textElement.ScaleText = isscale;
            if (symbol != null)
                textElement.Symbol = symbol;
            IElementProperties3 tempElement = textElement as IElementProperties3;
            tempElement.AnchorPoint = esriAnchorPointEnum.esriCenterPoint;
            tempElement.AutoTransform = true;
            tempElement.Name = elementName;
            tempElement.CustomProperty = elementProperty;
            IElement element = textElement as IElement;
            element.Geometry = geo;
            return element;
        }
        #endregion
    }
}
